import { Component, Inject } from 'vue-property-decorator';

import { mixins } from 'vue-class-component';
import JhiDataUtils from '@/shared/data/data-utils.service';

import { maxLength } from 'vuelidate/lib/validators';

import AlertService from '@/shared/alert/alert.service';

import { IRoomGoodSearch, RoomGoodSearch } from '@/shared/model/room-good-search.model';
import RoomGoodSearchService from './room-good-search.service';

const validations: any = {
  roomGoodSearch: {
    bkRoomType: {
      maxLength: maxLength(32),
    },
    bkNo: {
      maxLength: maxLength(32),
    },
    title: {},
    price: {
      maxLength: maxLength(32),
    },
    roomBasic: {},
    roomTransaction: {},
    rentBasic: {},
    communityId: {},
    roomFeatures: {},
    roomSellPoint: {},
    communityIntroduction: {},
    roomIntroduction: {},
    surroundingFacilities: {},
    transportation: {},
  },
};

@Component({
  validations,
})
export default class RoomGoodSearchUpdate extends mixins(JhiDataUtils) {
  @Inject('roomGoodSearchService') private roomGoodSearchService: () => RoomGoodSearchService;
  @Inject('alertService') private alertService: () => AlertService;

  public roomGoodSearch: IRoomGoodSearch = new RoomGoodSearch();
  public isSaving = false;
  public currentLanguage = '';

  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (to.params.roomGoodSearchId) {
        vm.retrieveRoomGoodSearch(to.params.roomGoodSearchId);
      }
    });
  }

  created(): void {
    this.currentLanguage = this.$store.getters.currentLanguage;
    this.$store.watch(
      () => this.$store.getters.currentLanguage,
      () => {
        this.currentLanguage = this.$store.getters.currentLanguage;
      }
    );
  }

  public save(): void {
    this.isSaving = true;
    if (this.roomGoodSearch.id) {
      this.roomGoodSearchService()
        .update(this.roomGoodSearch)
        .then(param => {
          this.isSaving = false;
          this.$router.go(-1);
          const message = 'A RoomGoodSearch is updated with identifier ' + param.id;
          return this.$root.$bvToast.toast(message.toString(), {
            toaster: 'b-toaster-top-center',
            title: 'Info',
            variant: 'info',
            solid: true,
            autoHideDelay: 5000,
          });
        })
        .catch(error => {
          this.isSaving = false;
          this.alertService().showHttpError(this, error.response);
        });
    } else {
      this.roomGoodSearchService()
        .create(this.roomGoodSearch)
        .then(param => {
          this.isSaving = false;
          this.$router.go(-1);
          const message = 'A RoomGoodSearch is created with identifier ' + param.id;
          this.$root.$bvToast.toast(message.toString(), {
            toaster: 'b-toaster-top-center',
            title: 'Success',
            variant: 'success',
            solid: true,
            autoHideDelay: 5000,
          });
        })
        .catch(error => {
          this.isSaving = false;
          this.alertService().showHttpError(this, error.response);
        });
    }
  }

  public retrieveRoomGoodSearch(roomGoodSearchId): void {
    this.roomGoodSearchService()
      .find(roomGoodSearchId)
      .then(res => {
        this.roomGoodSearch = res;
      })
      .catch(error => {
        this.alertService().showHttpError(this, error.response);
      });
  }

  public previousState(): void {
    this.$router.go(-1);
  }

  public initRelationships(): void {}
}
